package com.wfit.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wfit.config.JwtProperties;
import com.wfit.constant.AppUserCodeConstant;
import com.wfit.constant.MessageConstant;
import com.wfit.dto.AppUsersDto;
import com.wfit.dto.LoginFormDTO;
import com.wfit.dto.RegisterDto;
import com.wfit.exception.BadRequestException;
import com.wfit.mapper.AppUsersMapper;
import com.wfit.mapper.UsersMapper;
import com.wfit.po.AppUsers;
import com.wfit.po.Users;
import com.wfit.result.Result;
import com.wfit.service.AppUsersService;
import com.wfit.service.UsersService;
import com.wfit.utils.JwtTool;
import com.wfit.vo.UsersLoginVO;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

@Service
@RequiredArgsConstructor
public class AppUsersServiceImpl extends ServiceImpl<AppUsersMapper, AppUsers> implements AppUsersService {

    private final PasswordEncoder passwordEncoder;

    private final JwtTool jwtTool;

    private final JwtProperties jwtProperties;

    private final RedisTemplate redisTemplate;

    @Override
    public Result login(AppUsersDto dto) {
        // 1.数据校验

        String phone = dto.getPhonenumber();

        // 2.根据用户名或手机号查询
// TODO
        AppUsers users = null;


            users = lambdaQuery().eq(AppUsers::getPhonenumber, phone).one();
            if (users == null) {
                users = new AppUsers();
                users.setPhonenumber(phone);
                users.setCreateTime(new Date());
                users.setUpdateTime(new Date());
                users.setNickName(phone);
                users.setAmount(0.0);
                save(users);
            } else  {
                //        TODO
                if (users == null) {
                    return Result.error(MessageConstant.ERROR_PARAM);
                }
            }
            // 3.校验是否禁用
//        if (user.getStatus() == UserStatus.FROZEN) {
//            throw new ForbiddenException("用户被冻结");
//        }
            // 4.校验密码

//        TODO
        String o = (String)redisTemplate.opsForValue().get(AppUserCodeConstant.LOGIN_CODE + phone);
        if (!o.equalsIgnoreCase(dto.getImageCode())) {
            return Result.error(MessageConstant.CODE_ERROR);
        }



        // 5.生成TOKEN
        String token = jwtTool.createToken(users.getId(), jwtProperties.getTokenTTL());
        // 6.封装VO返回
        UsersLoginVO vo = new UsersLoginVO();
        vo.setId(users.getId());
        vo.setUserName(users.getNickName());
        vo.setToken(token);
        return Result.success(vo);
    }

    @Override
    public Result register(RegisterDto dto) {
        if (dto != null) {
            return Result.error(MessageConstant.PARAM_IS_NULL);
        }

        if (checkParam(dto)) return Result.error(MessageConstant.ERROR_PARAM);

        if (!(dto.getPassword() == dto.getRePassword())) {
            return Result.error(MessageConstant.PASSWORD_NOT_EQUAL_REPASSWORD);
        }
        Users users = new Users();
        users.setUserName(dto.getUsername());
        users.setPassword(passwordEncoder.encode(dto.getPassword()));
        return Result.success(MessageConstant.REGISTER_SUCCESS);
    }
    // 验证参数不为空
    private boolean checkParam(RegisterDto dto) {
        if(StringUtils.isBlank(dto.getUsername()) || StringUtils.isBlank(dto.getPassword()) || StringUtils.isBlank(dto.getRePassword())) {
            return true;
        }
        return false;
    }
}
